//
//  MLDHomeViewController.swift
//  Sina
//
//  Created by 万世豪情 on 15/10/9.
//  Copyright © 2015年 万世豪情. All rights reserved.
//

import UIKit

class MLDHomeViewController: UITableViewController, MLDPopViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupNav()
        
        
        
        
    }
    
    private func setupNav(){
        navigationItem.leftBarButtonItem = UIBarButtonItem.item("navigationbar_friendsearch", target: self, action: "push")
        navigationItem.rightBarButtonItem = UIBarButtonItem.item("navigationbar_pop", target: self, action: "push")
        
        setTitleButton()
    }
    
    //设置顶部按钮
    private func setTitleButton(){
        //初始化按钮
        let titleButton = MLDHomeTitleButton()
        
        //设置点击事件
        titleButton.addTarget(self, action: "titleButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        titleButton.backgroundColor = UIColor.lightGrayColor()
        
        //设置图片
        titleButton.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
        //设置select图片
        titleButton.setImage(UIImage(named: "navigationbar_arrow_up"), forState: UIControlState.Selected)
        
        //设置文字
        titleButton.setTitle("首页", forState: UIControlState.Normal)
        
        //设置文字颜色
        titleButton.setTitleColor(RGB(r: 80, g: 80, b: 80), forState: UIControlState.Normal)
        
        titleButton.sizeToFit()
        
        navigationItem.titleView = titleButton
        
        }
    
    
  
    
    @objc private func push(){
    let ctrl = MLDTempViewController()
        navigationController?.pushViewController(ctrl, animated: true)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //标题按钮点击事件
    @objc private func titleButtonClick(button: UIButton){
    
        button.selected = !button.selected
        
//        //添加遮罩视图
//        let coverButton = UIButton()
//        
//        coverButton.addTarget(self, action: "coverButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
//        coverButton.size = CGSizeMake(SCREENW, SCREENH)
//        coverButton.backgroundColor = UIColor(white: 0.5, alpha: 0.8)
//        
//        //拿到window
//        let window = UIApplication.sharedApplication().windows.last
//        window?.addSubview(coverButton)

        let contentView = UIView()
        
        contentView.backgroundColor = UIColor.orangeColor()
        contentView.size = CGSizeMake(150, 150)
        
        let view = MLDPopVIew(customView: contentView)
        view.delegate = self
        view.show(button)
//        printLog(self.view.frame)
        
    }
    //遮罩点击事件
    @objc private func coverButtonClick(button: UIButton){
        //移除遮罩
        button.removeFromSuperview()
        
    }
    func popViewWillDismiss() {
        //改变titleButton的状态
        let button = navigationItem.titleView! as! UIButton
        button.selected = false
    }

    
    

   
}













